//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FlatSmileSection.h"
using namespace Cephei::QL::Termstructures::Volatility;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::CFlatSmileSection::CFlatSmileSection (Double exerciseTime, Double vol, Cephei::QL::Times::IDayCounter^ dc, Microsoft::FSharp::Core::FSharpOption<Double>^ atmLevel) : CSmileSection(CFlatSmileSection::typeid)
{
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phFlatSmileSection = NULL;
#endif
        QuantLib::Time _exerciseTime = (QuantLib::Time)ValueHelper::Convert (exerciseTime);
        QuantLib::Volatility _vol = (QuantLib::Volatility)ValueHelper::Convert (vol);
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        QuantLib::Real _atmLevel = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (atmLevel) ? (QuantLib::Real)ValueHelper::Convert (atmLevel->Value) : Null<QuantLib::Rate>()); //4
        _ppFlatSmileSection = new boost::shared_ptr<QuantLib::FlatSmileSection> (new QuantLib::FlatSmileSection ( _exerciseTime,  _vol,  _dc,  _atmLevel ));
        SetSmileSection (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppFlatSmileSection));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::CFlatSmileSection::CFlatSmileSection (DateTime d, Double vol, Cephei::QL::Times::IDayCounter^ dc, Microsoft::FSharp::Core::FSharpOption<DateTime>^ referenceDate, Microsoft::FSharp::Core::FSharpOption<Double>^ atmLevel) : CSmileSection(CFlatSmileSection::typeid)
{
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phFlatSmileSection = NULL;
#endif
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
        QuantLib::Volatility _vol = (QuantLib::Volatility)ValueHelper::Convert (vol);
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        QuantLib::Date _referenceDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (referenceDate) ? (QuantLib::Date)ValueHelper::Convert (referenceDate->Value) : QuantLib::Date()); //4
        QuantLib::Real _atmLevel = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (atmLevel) ? (QuantLib::Real)ValueHelper::Convert (atmLevel->Value) : Null<QuantLib::Rate>()); //4
        _ppFlatSmileSection = new boost::shared_ptr<QuantLib::FlatSmileSection> (new QuantLib::FlatSmileSection ( _d,  _vol,  _dc,  _referenceDate,  _atmLevel ));
        SetSmileSection (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppFlatSmileSection));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::CFlatSmileSection::CFlatSmileSection (boost::shared_ptr<QuantLib::FlatSmileSection>& childNative, Object^ owner) : CSmileSection(CFlatSmileSection::typeid)
{
#ifdef HANDLE
	_phFlatSmileSection = NULL;
#endif
	_ppFlatSmileSection = &childNative;
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppFlatSmileSection));
}
Cephei::QL::Termstructures::Volatility::CFlatSmileSection::CFlatSmileSection (QuantLib::FlatSmileSection& childNative, Object^ owner) : CSmileSection(CFlatSmileSection::typeid)
{
#ifdef HANDLE
	_phFlatSmileSection = NULL;
#endif
	_ppFlatSmileSection = new boost::shared_ptr<QuantLib::FlatSmileSection> (&childNative);
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppFlatSmileSection));
    _FlatSmileSectionOwner = owner;
    _SmileSectionOwner = owner;
}

Cephei::QL::Termstructures::Volatility::CFlatSmileSection::CFlatSmileSection (CFlatSmileSection^ copy) : CSmileSection(CFlatSmileSection::typeid)
{
#ifdef HANDLE
	_phFlatSmileSection = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFlatSmileSection = new boost::shared_ptr<QuantLib::FlatSmileSection> (copy->GetShared());
        _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppFlatSmileSection));
    }
}
Cephei::QL::Termstructures::Volatility::CFlatSmileSection::CFlatSmileSection (System::Type^ t) : CSmileSection(CFlatSmileSection::typeid)
{
#ifdef HANDLE
	_phFlatSmileSection = NULL;
#endif
	if (!t->IsSubclassOf(CFlatSmileSection::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::CFlatSmileSection::CFlatSmileSection (QuantLib::Handle<QuantLib::FlatSmileSection>& childNative, Object^ owner)  : CSmileSection(CFlatSmileSection::typeid)
{
	_phFlatSmileSection = &childNative;
	_ppFlatSmileSection = &static_cast<boost::shared_ptr<QuantLib::FlatSmileSection>>(childNative.currentLink());
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppFlatSmileSection));
    _FlatSmileSectionOwner = owner;
}
Cephei::QL::Termstructures::Volatility::CFlatSmileSection::CFlatSmileSection (QuantLib::Handle<QuantLib::FlatSmileSection> childNative)  : CSmileSection(CFlatSmileSection::typeid)
{
	_phFlatSmileSection = &childNative;
	_ppFlatSmileSection = &static_cast<boost::shared_ptr<QuantLib::FlatSmileSection>>(childNative.currentLink());
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppFlatSmileSection));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::CFlatSmileSection::CFlatSmileSection (QuantLib::FlatSmileSection childNative)  : CSmileSection(CFlatSmileSection::typeid)
{
#ifdef HANDLE
	_phFlatSmileSection = NULL;
#endif
	_ppFlatSmileSection = new boost::shared_ptr<QuantLib::FlatSmileSection> (new QuantLib::FlatSmileSection (childNative));
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppFlatSmileSection));
}
#endif

Cephei::QL::Termstructures::Volatility::CFlatSmileSection::~CFlatSmileSection ()
{
    if (_ppFlatSmileSection != NULL)
    {
	    delete _ppFlatSmileSection;
        _ppFlatSmileSection = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::CFlatSmileSection::!CFlatSmileSection ()
{
    if (_ppFlatSmileSection != NULL)
    {
	    delete _ppFlatSmileSection;
    }
}
QuantLib::FlatSmileSection& Cephei::QL::Termstructures::Volatility::CFlatSmileSection::GetReference ()
{
    if (_ppFlatSmileSection == NULL) throw gcnew NativeNullException ();
	return **_ppFlatSmileSection;
}
boost::shared_ptr<QuantLib::FlatSmileSection>& Cephei::QL::Termstructures::Volatility::CFlatSmileSection::GetShared ()
{
    if (_ppFlatSmileSection == NULL) throw gcnew NativeNullException ();
	return *_ppFlatSmileSection;
}
QuantLib::FlatSmileSection* Cephei::QL::Termstructures::Volatility::CFlatSmileSection::GetPointer ()
{
    if (_ppFlatSmileSection == NULL) throw gcnew NativeNullException ();
	return &**_ppFlatSmileSection;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FlatSmileSection>& Cephei::QL::Termstructures::Volatility::CFlatSmileSection::GetHandle ()
{
	if (_phFlatSmileSection == NULL)
	{
		_phFlatSmileSection = new Handle<QuantLib::FlatSmileSection> (*_ppFlatSmileSection);
	}
	return *_phFlatSmileSection;
}
#endif
bool Cephei::QL::Termstructures::Volatility::CFlatSmileSection::HasNative () 
{
	return (_ppFlatSmileSection != NULL);
}

Double Cephei::QL::Termstructures::Volatility::CFlatSmileSection::AtmLevel::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFlatSmileSection)->atmLevel ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CFlatSmileSection::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFlatSmileSection)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CFlatSmileSection::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFlatSmileSection)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::IFlatSmileSection^ Cephei::QL::Termstructures::Volatility::CFlatSmileSection_Factory::Create (Double exerciseTime, Double vol, Cephei::QL::Times::IDayCounter^ dc, Microsoft::FSharp::Core::FSharpOption<Double>^ atmLevel)
{
    return gcnew CFlatSmileSection ( exerciseTime,  vol,  dc,  atmLevel);
}
Cephei::QL::Termstructures::Volatility::IFlatSmileSection^ Cephei::QL::Termstructures::Volatility::CFlatSmileSection_Factory::Create (DateTime d, Double vol, Cephei::QL::Times::IDayCounter^ dc, Microsoft::FSharp::Core::FSharpOption<DateTime>^ referenceDate, Microsoft::FSharp::Core::FSharpOption<Double>^ atmLevel)
{
    return gcnew CFlatSmileSection ( d,  vol,  dc,  referenceDate,  atmLevel);
}
